package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItem;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemList;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntryList;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * Author: Phani Raja Kumar
 * Date  : Feb 20, 2005
 * Time  : 12:58:39 PM
 */
public class PurchaseOrderLineItemLotDataEntryAddCommand extends ChipTrackExtendedAction
{

    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {

        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        PurchaseOrderForm purchaseOrderForm = ( PurchaseOrderForm ) form;
		String actionForward 		= "Search";
        HttpSession mSession 		= request.getSession();
        PurchaseOrderLineItem purchaseOrderLineItem;
        PurchaseOrderLineItemList purchaseOrderLineItemList;
        PurchaseOrderLineItemLotDataEntryList purchaseOrderLineItemLotDataEntryList;

        int purchaseOrderLineItemNumber = -1;
        Iterator lineItemIterator;

		try
        {
            purchaseOrderLineItemNumber = Integer.parseInt( request.getParameter("purchaseOrderLineItemNumber") );
            purchaseOrderLineItemList = purchaseOrderForm.getPurchaseOrderLineItemList();
            lineItemIterator = purchaseOrderLineItemList.iterator();

            //for assigning New Purchase Order Line Item Data Entry to the selected Purchase Order Line Item, if it's is empty.
            while ( lineItemIterator.hasNext() )
            {
                purchaseOrderLineItem = ( PurchaseOrderLineItem ) lineItemIterator.next();

                // If Selected Line item index and Line Item Index match then set values of Purchasing Lot Number List.
                if ( purchaseOrderLineItem.getPurchaseOrderLineItemNumber() == purchaseOrderLineItemNumber )
                {
                    purchaseOrderLineItemLotDataEntryList = purchaseOrderLineItem.getPurchaseOrderLineItemLotDataEntryList();
                    purchaseOrderLineItemLotDataEntryList.add( new PurchaseOrderLineItemLotDataEntry() );
                    purchaseOrderLineItem.setPurchaseOrderLineItemLotDataEntryList( purchaseOrderLineItemLotDataEntryList );
                }
            }
            actionForward = "purchaseOrderLineItemLotEntry";
       	}
        catch ( Exception e )
        {
            Logger.error( ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY, e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY, e ) );
            actionForward = "Search";
        }

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
    }
}
